import { GENDER_SELECT_LIST } from '@/pages/supplier/constants/common'
import { list2Obj } from '@/pages/supplier/utils';
const GENDER_MAP_OBJECT = list2Obj(GENDER_SELECT_LIST)
const getInitialFieldsValue = (list) => {
  const obj = {}
  list.forEach(({ field }) => {
    obj[field] = ''
  });
  return obj
}

const SUPPLIER_STAFF_TABLE_COLUMNS = [
  { title: '序号', type: 'seq', width: 50 },
  {
    field: 'personName',
    title: '姓名',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.name-input .vxe-input--inner' },
    slots: { edit: 'name_edit' }
  },
  {
    field: 'personSex',
    title: '性别',
    width: 150,
    editRender: { placeholder: '请点击选择...' },
    slots: { edit: 'sex_edit', default: 'sex_default' }
  },
  {
    field: 'idCard',
    title: '身份证号',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.id-input .vxe-input--inner' },
    slots: { edit: 'id_edit' }
  },
  {
    field: 'personPost',
    title: '职务',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.role-input .vxe-input--inner' },
    slots: { edit: 'post_edit' }
  },
  {
    field: 'personRole',
    title: '角色',
    width: 150,
    editRender: { placeholder: '请点击选择...' },
    slots: { edit: 'role_edit', default: 'role_default' }
  },
  {
    field: 'contactPhone',
    title: '联系电话',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.phone-input .vxe-input--inner' },
    slots: { edit: 'phone_edit' }
  },
  {
    field: 'directSuperior',
    title: '直属上级',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.superior-input .vxe-input--inner' },
    slots: { edit: 'superior_edit' }
  },
  {
    field: 'managementScope',
    title: '管理范围',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.scope-input .vxe-input--inner' },
    slots: { edit: 'scope_edit' }
  },
  {
    field: 'wxQq',
    title: '微信/QQ',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.wxqq-input .vxe-input--inner' },
    slots: { edit: 'wxqq_edit' }
  },
  {
    field: 'email',
    title: 'E-mail',
    width: 150,
    editRender: { placeholder: '请点击输入...', autofocus: '.email-input .vxe-input--inner' },
    slots: { edit: 'email_edit' }
  },
  { title: '操作', slots: { default: 'actions' }, width: 80, },
]
const INITIAL_SUPPLIER_FIELDS_VALUE = getInitialFieldsValue(SUPPLIER_STAFF_TABLE_COLUMNS.filter(i => i.field))
const STAFF_TABLE_COLUMNS = [
  { title: '序号', type: 'seq', width: 50 },
  {
    field: 'personName',
    title: '姓名',
    minWidth: '100px',
    slots: { default: 'name' }
  },
  {
    field: 'sex',
    title: '性别',
    formatter: ({ cellValue, row, column }) => {
      return GENDER_MAP_OBJECT[cellValue]
    },
    // editRender: { placeholder: '请点击选择性别', },
    // slots: { edit: 'sex_edit' }
  },
  {
    field: 'jobNo',
    title: '工号',
  },
  {
    field: 'personPost',
    title: '职务',
  },
  {
    field: 'personRole',
    title: '角色',
    // formatter: ({ cellValue, row, column }) => {
    //   return JOB_TITLE_MAP_OBJECT[cellValue]
    // },
    editRender: { placeholder: '请点击选择...' },
    slots: { edit: 'role_edit', default: 'role_default' }
  },
  {
    field: 'belongArea',
    title: '管理范围',
  },
  {
    field: 'contactPhone',
    title: '联系电话',
  },
  { title: '操作', slots: { default: 'actions' }, width: 80, },
  // {
  //   field: 'wxQq',
  //   title: '微信/QQ',
  //   width: 150,
  //   editRender: { placeholder: '请点击输入...', autofocus: '.wxqq-input .vxe-input--inner' },
  //   slots: { edit: 'wxqq_edit' }
  // },
  // {
  //   field: 'email',
  //   title: 'E-mail',
  //   width: 150,
  //   editRender: { placeholder: '请点击输入...', autofocus: '.email-input .vxe-input--inner' },
  //   slots: { edit: 'email_edit' }
  // },
]
const INITIAL_STAFF_FIELDS_VALUE = getInitialFieldsValue(STAFF_TABLE_COLUMNS.filter(i => i.field))

const getSupplierStaffTableColumns = (isQualified, readonly) => {
  if (isQualified && !SUPPLIER_STAFF_TABLE_COLUMNS.some(i => i.field === 'serviceAreaList')) {
    SUPPLIER_STAFF_TABLE_COLUMNS.splice(8, -1, {
      field: 'serviceAreaList',
      title: '服务区域',
      width: 150,
      formatter: ({ cellValue }) => {
        if (!cellValue) {
          return ''
        }
        return cellValue.map(({ itemName }) => {
          return itemName
        }).join(', ')
      },
      editRender: { placeholder: '请点击选择...' },
      slots: { edit: 'service_area_edit', default: 'service_area_default' }
    })
  }
  if (readonly) {
    return SUPPLIER_STAFF_TABLE_COLUMNS
      .filter(item => item.title !== '操作')
      .map((item) => {
        const obj = Object.create(null)
        for (let [key, value] of Object.entries(item)) {
          if (key === 'slots' && value.default) {
            obj[key] = { default: value.default }
          } else if (key != 'editRender') {
            obj[key] = value
          }
        }
        return obj
      })
  }
  return SUPPLIER_STAFF_TABLE_COLUMNS
}
const getInitialSupplierFieldsValue = (isQualified, readonly) => {
  if (isQualified) {
    INITIAL_SUPPLIER_FIELDS_VALUE['serviceAreaList'] = []
    INITIAL_SUPPLIER_FIELDS_VALUE['serviceAreaIds'] = []
    INITIAL_SUPPLIER_FIELDS_VALUE['serviceArea'] = ''
  }
  return INITIAL_SUPPLIER_FIELDS_VALUE
}

const getStaffTableColumns = (readonly) => {
  if (readonly) {
    return STAFF_TABLE_COLUMNS
      .filter(item => item.title !== '操作')
      .map((item) => {
        const obj = Object.create(null)
        for (let [key, value] of Object.entries(item)) {
          if (key === 'slots' && value.default) {
            obj[key] = { default: value.default }
          } else if (key != 'editRender') {
            obj[key] = value
          }
        }
        return obj
      })
  }
  return STAFF_TABLE_COLUMNS
}

export default {
  getSupplierStaffTableColumns,
  getInitialSupplierFieldsValue,
  getStaffTableColumns,
  INITIAL_STAFF_FIELDS_VALUE,
  // STAFF_TABLE_COLUMNS,
}
